<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Blog/classes/PhpFox_ComponentEditBlog.class.php, $Author:  $ */

/** Component for editing user blog
 *
 * @version $Id: PhpFox_ComponentEditBlog.class.php 133 2007-12-28 15:43:39Z phpfoxsv $
 * @package module.blog
 */
class PhpFox_Mod_Blog_ComponentEditBlog extends BaseComponent
{
    function process()
    {
        $oBlogItem = $this->_oModule->getItem('Blog');
        /* @var $oBlogItem PhpFox_Mod_Blog_ItemBlog */
        $oSecurityService = App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        $oTpl = $this->_oModule->getTemplate();
        App::loadClass('Validator');
        App::loadClass('Text');
        $oTxt = &new Text();

        $oSecurityService->checkPerm('blog', 'full');
        $oSecurityService->checkAccess('blogs');
        $sUser = $oSecurityService->getCurrentUserLogin();
        if (!$iId = $oReq->getInt('id'))
        {
            App::gotoUrl('public.account.myitems.blog');
        }
        $oBlogItem->load($iId);
        if ($oBlogItem->aData['user_id'] != App::getUser('id'))
        {
            App::gotoUrl('public.account.myitems.blog');
        }
        $aFields = array
        (
        	'val[title]' => array('title' => App::format('Mod_Blog.add_form_title'), 'def' => 'required'),
        );
        $oValidator = &new Validator($aFields, array());
        if ($oReq->getAction() == 'edit')
        {
            if (!$oValidator->isValid($oReq->getAll()))
            {
                $this->addErrors($oValidator->getErrors());
            }
            $aValues = $oReq->getArray('val');
            if (!$aValues['text'])
            {
                $this->addErrors(App::format('Mod_Blog.error.no_content'));
            }
            if (!count($this->getErrors()))
            {
                $aValues['title'] 	   = $oTxt->_splitString($aValues['title'], 30);
	    	    $oBlogItem->aData['title']     = $oTxt->preParseText($aValues['title']);
                $oBlogItem->aData['text']      = $oTxt->preParseText($oTxt->parseText($aValues['text'],0,0,1));
                $oBlogItem->aData['tags']      = strip_tags($oTxt->preParseText($oTxt->ShortenText($aValues['tags'], 75)));
                $oBlogItem->update();
                App::gotoUrl('public.account.myitems.blog.edit', array('id' => $iId, 'preview' => $iId));
            }
        }

        $aItem = ($oReq->getAction() == 'edit') ? $aValues : $oBlogItem->aData;
        $oTpl->assignSrc('aItem', $aItem);
        $oTpl->assignSrc('sValMess', $oValidator->makeJsMess());
        $oTpl->assignSrc('sValJs', $oValidator->makeJS());
        $oTpl->assign(array(
        	'aErrors'  => $this->getErrors(),
        	'bPreview' => $oReq->get('preview'),
        ));
        return $oTpl->fetch('EditBlog.html');
    }
}
?>